package com.softeem.music_sys.controller;

import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.User;
import com.softeem.music_sys.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 */
@Tag(name = "用户接口", description = "用户相关操作:注册、登录、信息管理等")
@RestController
@RequestMapping("/user")
public class UserController {
    
    @Autowired
    private UserService userService;

    @Operation(summary = "用户注册", description = "用户注册，提供用户信息")
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        return userService.register(user);
    }
    
    @Operation(summary = "用户注册（直接POST）", description = "用户注册，提供用户信息")
    @PostMapping
    public Result addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    @Operation(summary = "用户登录（GET）", description = "用户登录，提供用户名和密码")
    @Parameters({
            @Parameter(name = "username", description = "用户名"),
            @Parameter(name = "password", description = "密码")
    })
    @GetMapping("/login")
    public Result login(String username, String password) {
        return userService.login(username, password);
    }
    
    @Operation(summary = "用户登录（POST）", description = "用户登录，提供用户名和密码")
    @PostMapping("/login")
    public Result loginPost(@RequestBody User loginUser) {
        return userService.login(loginUser.getUsername(), loginUser.getPassword());
    }
    
    @Operation(summary = "创建测试用户", description = "创建一个测试账号 admin/123456")
    @GetMapping("/createTestUser")
    public Result createTestUser() {
        return userService.createTestUser();
    }

    @Operation(summary = "获取用户信息", description = "根据ID获取用户详细信息")
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Integer id) {
        return userService.getUserById(id);
    }

    @Operation(summary = "更新用户信息", description = "更新用户信息")
    @PutMapping
    public Result update(@RequestBody User user) {
        return userService.update(user);
    }

    @Operation(summary = "删除用户", description = "根据ID删除用户")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return userService.delete(id);
    }

    @Operation(summary = "获取用户列表", description = "分页获取用户列表")
    @Parameters({
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "limit", description = "每页数量")
    })
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "10") Integer limit) {
        return userService.getUserList(page, limit);
    }

    @Operation(summary = "搜索用户", description = "根据用户名搜索用户")
    @GetMapping("/search")
    public Result search(@RequestParam String username,
                        @RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "10") Integer limit) {
        return userService.searchUsersController(username, page, limit);
    }

    @Operation(summary = "禁用/启用用户", description = "修改用户状态")
    @Parameters({
            @Parameter(name = "id", description = "用户ID"),
            @Parameter(name = "status", description = "状态 1-启用 0-禁用")
    })
    @PutMapping("/status/{id}")
    public Result updateStatus(@PathVariable Integer id, @RequestParam Integer status) {
        return userService.updateStatusController(id, status);
    }
}